clear;clc;clf; close all;

%% 两对meta-atom
%%
mws = CST_Initilization;
tic;
CST_Modeling(mws);
disp(["建模成功，所用时间为：", num2str(toc), 'S']);
% CST_Parameter_Sweep(mws, 'Dx', -30, 30, 10);
% CST_Parameter_Sweep(mws, 'Dy', -20, 10, 5);
% invoke(invoke(mws, 'FDSolver'), 'Start');
% release(mws);
% release(cst);

%% 调用CSTStudio并初始化
function mws = CST_Initilization()
    % path = pwd; % 获取当前m文件夹路径
    cst = actxserver('CSTStudio.application');
    mws = invoke(cst, 'NewMWS');
    % app = invoke(mws, 'GetApplicationName');
    % ver = invoke(mws, 'GetApplicationVersion');
    invoke(mws, 'FileNew');
    invoke(mws, 'DeleteResults'); % 删除之前的结果，防止程序停止运行

end

%% 建模函数
function [] = CST_Modeling(mws)

    Frq = [0.5, 2]; % 工作频率,太赫兹波波长范围是3mm-30um
    FC_Property_Setup_Simplfied(mws, Frq);
    % 仿真参数设置
    Px = 350; %结构X向边长
    Py = 320; %结构Y向边长
    t_metal = 0.2; %基地层金属厚度
    t_dielectric = 25;
    L1 = 100; W1 = 35; Dx = 0; Sx = Px / 4;
    L2 = 100; W2 = 35; Dy = 0; Sy = Py / 4;

    % 在CST中加入结构参数，方便后续查看
    invoke(mws, 'StoreParameter', 'Px', Px);
    invoke(mws, 'StoreParameter', 'Py', Py);
    invoke(mws, 'StoreParameter', 't_metal', t_metal);
    invoke(mws, 'StoreParameter', 't_dielectric', t_dielectric);
    invoke(mws, 'StoreParameterWithDescription', 'L1 ', L1, '长度不能超过周期Py的一半');
    invoke(mws, 'StoreParameterWithDescription', 'L2 ', L2, '长度不能超过周期Py的一半');
    invoke(mws, 'StoreParameter', 'W1 ', W1);
    invoke(mws, 'StoreParameter', 'W2 ', W2);
    invoke(mws, 'StoreParameter', 'Dx ', Dx);
    invoke(mws, 'StoreParameter', 'Dy ', Dy);
    invoke(mws, 'StoreParameter', 'Sx ', Sx);
    invoke(mws, 'StoreParameter', 'Sy ', Sy);
    % CST_Post = CST_MicrowaveStudio();
    % CST_Post.addParameter('t', 100);

    % 组件
    FC_New_Component(mws, "Ground");
    FC_New_Component(mws, "介质");
    FC_New_Component(mws, "Metal");

    % 建立模型
    % 1. 建立底层基底金属模型
    % 建立方块的函数

    FC_Model_Brick(mws, '-Px / 2', 'Px / 2', '-Py / 2', 'Py / 2', '-t_metal', '0', 'Gold_YW', 'Ground', '基底');

    % 2. 建立meta-atoms
    % 第一组meta-atom
    FC_Model_Brick(mws, '-Sx-W2/2', '-Sx+W2/2', '-Sy-L2/2', '-Sy+L2/2', '0', 't_dielectric', 'HTL_YW', '介质', 'Left_Bar');
    FC_Model_Brick(mws, '+Sx-W1/2+Dx', '+Sx+W1/2+Dx', '+Sy-L1/2+Dy', '+Sy+L1/2+Dy', '0', 't_dielectric', 'HTL_YW', '介质', 'Right_Bar');

    FC_Model_Brick(mws, '-Sx-W2/2', '-Sx+W2/2', '-Sy-L2/2', '-Sy+L2/2', 't_dielectric', 't_dielectric+t_metal', 'Gold_YW', 'Metal', 'Top_Left_Bar');
    FC_Model_Brick(mws, '+Sx-W1/2+Dx', '+Sx+W1/2+Dx', '+Sy-L1/2+Dy', '+Sy+L1/2+Dy', 't_dielectric', 't_dielectric+t_metal', 'Gold_YW', 'Metal', 'Top_Right_Bar');
    % 第二组meta-atom
    FC_Model_Brick(mws, '-Sx-L1/2', '-Sx+L1/2', '-(-Sy-W1/2)', '-(-Sy+W1/2)', '0', 't_dielectric', 'HTL_YW', '介质', 'Left_Bar_2');
    FC_Model_Brick(mws, '+Sx-L2/2+Dy', '+Sx+L2/2+Dy', '-(+Sy-W2/2)-Dx', '-(+Sy+W2/2)-Dx', '0', 't_dielectric', 'HTL_YW', '介质', 'Right_Bar_2');

    FC_Model_Brick(mws, '-Sx-L1/2', '-Sx+L1/2', '-(-Sy-W1/2)', '-(-Sy+W1/2)', 't_dielectric', 't_dielectric+t_metal', 'Gold_YW', 'Metal', 'Top_Left_Bar_2');
    FC_Model_Brick(mws, '+Sx-L2/2+Dy', '+Sx+L2/2+Dy', '-(+Sy-W2/2)-Dx', '-(+Sy+W2/2)-Dx', 't_dielectric', 't_dielectric+t_metal', 'Gold_YW', 'Metal', 'Top_Right_Bar_2');

end

%% 后处理函数
function CST_Post_Process(mws)

    CST_Post = CST_MicrowaveStudio();
    [freq, sparam, stype] = CST_Post.getSParameters;
    % 得到的S参数依次为：
    % Szmax1,zmax1    Szmax2,zmax1    Szmin1,zmax1    Szmin2,max1
    % Szmax1,zmax2    Szmax2,zmax2    Szmin1,zmax2    Szmin2,zmax2
    % Szmax1,zmin1     Szmax2,zmin1     Szmin1,zmin1    Szmin2,zmin1
    % Szmax1,zmin2     Szmax2,zmin2    Szmin1,zmin2    Szmin2,zmin2
    % Szmax1,zmax1 == Szmax2,zmax2
    % 取CD = Szmax2,zmax1-Szmax1,zmax2
    % frequency = freq(:,1); % 获取频率点
    % save('frequency.mat','frequency');
    real = real(sparam); % 获取实部
    imag = imag(sparam); % 获取虚部
    linear = sqrt(real .* real + imag .* imag); % 计算linear值
    Szmax1zmax1 = linear(:, 1);
    Szmax2zmax1 = linear(:, 2);
    Szmax1zmax2 = linear(:, 5);
    CD = Szmax2zmax1 .* Szmax2zmax1 - Szmax1zmax2 .* Szmax1zmax2;
    real_name = [path, '\result\real\real_', num2str(n), '.mat'];
    imag_name = [path, '\result\imag\imag_', num2str(n), '.mat'];
    Szmax1zmax1_name = [path, '\result\S-Parameters\Szmax1zmax1_', num2str(n), '.mat'];
    Szmax2zmax1_name = [path, '\result\S-Parameters\Szmax2zmax1_', num2str(n), '.mat'];
    Szmax1zmax2_name = [path, '\result\S-Parameters\Szmax1zmax2_', num2str(n), '.mat'];
    CD_name = [path, '\result\CD\CD_', num2str(n), '.mat'];
    save(real_name, 'real');
    save(imag_name, 'imag');
    save(Szmax1zmax1_name, 'Szmax1zmax1');
    save(Szmax2zmax1_name, 'Szmax2zmax1');
    save(Szmax1zmax2_name, 'Szmax1zmax2');
    save(CD_name, 'CD')
    clear freq sparam stype real imag linear Szmax1zmax1 Szmax2zmax1 Szmax1zmax2 CD
    % 保存并退出
    %     % invoke(mws,'Save');
    %     filename = ['\metamaterials\metamaterial', num2str(n), '.cst']; % 新建的CST文件名字
    %     fullname = [path filename];
    %     invoke(mws, 'SaveAs', fullname, 'True'); % 保存到目前为止的结果
    %     invoke(mws, 'Quit');

end

%% 保存文件
function [] = CST_Save_File(mws, Path, Filename)
    fullname = [Path Filename];
    invoke(mws, 'SaveAs', fullname, 'True'); %True表示保存到目前为止的结果
end

function [] = CST_Parameter_Sweep(mws, Para, From_, To_, StepWidth_)
    % Para = 'Dy'; From_ = 0; To_ = 12; StepWidth_ = 4;
    CMDs = '';
    CMDs = [CMDs 'With ParameterSweep'];
    CMDs = [CMDs 10 '  .DeleteAllSequences'];
    %     CMDs = [CMDs 10 '    .SetSimulationType ("Frequency")'];
    CMDs = [CMDs 10 ' .AddSequence ("Sweep_' Para '")'];
    CMDs = [CMDs 10 ' .AddParameter_Stepwidth ("Sweep_' Para '", "' Para '", ' num2str(From_) ', ' num2str(To_) ', ' num2str(StepWidth_) ')'];
    %     CMDs = [CMDs 10 '    .Start'];
    CMDs = [CMDs 10 'End With']; % CMDs
    invoke(mws, 'AddToHistory', ['定义参数化扫描_' Para], CMDs);

end
